﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Classical Gamma filter (RGB)
    /// </summary>
    public class Gamma : BaseImageFilter
    {
        protected double red = 0.0f;
        protected double green = 0.0f;
        protected double blue = 0.0f;

        /// <summary>
        /// Initializes a new instance of the <see cref="Gamma"/> class.
        /// </summary>
        public Gamma()
            : this(0.75f, 0.75f, 0.75f)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Gamma"/> class.
        /// </summary>
        /// <param name="_red">The _red.</param>
        /// <param name="_green">The _green.</param>
        /// <param name="_blue">The _blue.</param>
        public Gamma(double _red, double _green, double _blue)
        {
            red = _red;
            green = _green;
            blue = _blue;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Gamma"/> class.
        /// </summary>
        /// <param name="_gamma">The _gamma.</param>
        internal Gamma(Gamma _gamma)
        {
            this.red = _gamma.Red;
            this.green = _gamma.Green;
            this.blue = _gamma.Blue;
        }

        /// <summary>
        /// Gets or sets the red.
        /// </summary>
        /// <value>The red.</value>
        public double Red
        {
            get { return red;  }
            set { red = value; }
        }

        /// <summary>
        /// Gets or sets the green.
        /// </summary>
        /// <value>The green.</value>
        public double Green
        {
            get { return green; }
            set { green = value; }
        }

        /// <summary>
        /// Gets or sets the blue.
        /// </summary>
        /// <value>The blue.</value>
        public double Blue
        {
            get { return blue; }
            set { blue = value; }
        }

        /// <summary>
        /// Change gamma for Bitmap
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            if (red < .2 || red > 5) return _bitmap;
            if (green < .2 || green > 5) return _bitmap;
            if (blue < .2 || blue > 5) return _bitmap;

            byte[] redGamma = new byte[256];
            byte[] greenGamma = new byte[256];
            byte[] blueGamma = new byte[256];

            for (int i = 0; i < 256; ++i)
            {
                redGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / red)) + 0.5));
                greenGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / green)) + 0.5));
                blueGamma[i] = (byte)Math.Min(255, (int)((255.0 * Math.Pow(i / 255.0, 1.0 / blue)) + 0.5));
            }

            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = bitmap.BitmapData; //bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            IntPtr Scan0 = bmData.Scan0;

            unsafe
            {
                int nWidth = bitmap.Width;
                int nHeight = bitmap.Height;
                byte* p = (byte*)(void*)Scan0;

                int nBytesPerPixelCount = bitmap.BytesPerPixelCount;
                int nOffset = stride - nWidth * nBytesPerPixelCount; //stride - nWidth * 3;
                for (int y = 0; y < nHeight; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        p[2] = redGamma[p[2]];
                        p[1] = greenGamma[p[1]];
                        p[0] = blueGamma[p[0]];

                        p += nBytesPerPixelCount;
                    }
                    p += nOffset;
                }
            }

            return _bitmap; 
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Gamma(this);
        }
    }
}
